package com.xj.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

/**
 * 与具体ORM实现无关的分页参数及查询结果封装.
 * 
 * 注意所有序号从1开始.
 * 
 * @param <T> Page中记录的类型.
 * 
 * @author Robin
 */
public class Page<T> {
    //-- 公共变量 --//
    public static final String ASC = "asc";
    public static final String DESC = "desc";

    //-- 分页参数 --//
    protected int pageNo = 1;//当前页数
    protected int pageSize = -1;//一页显示几条记录
    protected String orderBy = null;
    protected String order = null;
    protected boolean autoCount = true;

    //-- 返回结果 --//
    protected List<T> result = Lists.newArrayList();
    protected long totalCount = -1; //总条数
//    protected long totalPages =1;//总页数

    //-- 构造函数 --//
    public Page() {
    }

    public Page(int pageSize) {
        this.pageSize = pageSize;
    }

    //-- 分页参数访问函数 --//
    /**
     * 获得当前页的页号,序号从1开始,默认为1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * 返回Page对象自身的setPageNo函数,可用于连续设置。
     */
    public Page<T> pageNo(final int thePageNo) {
        setPageNo(thePageNo);
        return this;
    }

    /**
     * 获得每页的记录数量, 默认为-1.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页的记录数量.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 返回Page对象自身的setPageSize函数,可用于连续设置。
     */
    public Page<T> pageSize(final int thePageSize) {
        setPageSize(thePageSize);
        return this;
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public int getFirst() {
        return ((pageNo - 1) * pageSize) + 1;
    }

    /**
     * 获得排序字段,无默认值. 多个排序字段时用','分隔.
     */
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * 设置排序字段,多个排序字段时用','分隔.
     */
    public void setOrderBy(final String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * 返回Page对象自身的setOrderBy函数,可用于连续设置。
     */
    public Page<T> orderBy(final String theOrderBy) {
        setOrderBy(theOrderBy);
        return this;
    }

    /**
     * 获得排序方向, 无默认值.
     */
    public String getOrder() {
        return order;
    }

    /**
     * 设置排序方式向.
     * 
     * @param order 可选值为desc或asc,多个排序字段时用','分隔.
     */
    public void setOrder(final String order) {
        String lowcaseOrder = StringUtils.lowerCase(order);

        //检查order字符串的合法值
        String[] orders = StringUtils.split(lowcaseOrder, ',');
        for (String orderStr : orders) {
            if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
                throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
            }
        }

        this.order = lowcaseOrder;
    }

    /**
     * 返回Page对象自身的setOrder函数,可用于连续设置。
     */
    public Page<T> order(final String theOrder) {
        setOrder(theOrder);
        return this;
    }

    /**
     * 是否已设置排序字段,无默认值.
     */
    public boolean isOrderBySetted() {
        return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
    }

    /**
     * 获得查询对象时是否先自动执行count查询获取总记录数, 默认为false.
     */
    public boolean isAutoCount() {
        return autoCount;
    }

    /**
     * 设置查询对象时是否自动先执行count查询获取总记录数.
     */
    public void setAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }

    /**
     * 返回Page对象自身的setAutoCount函数,可用于连续设置。
     */
    public Page<T> autoCount(final boolean theAutoCount) {
        setAutoCount(theAutoCount);
        return this;
    }

    //-- 访问查询结果函数 --//

    /**
     * 获得页内的记录列表.
     */
    public List<T> getResult() {
        return result;
    }

    /**
     * 设置页内的记录列表.
     */
    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * 获得总记录数, 默认值为-1.
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * 设置总记录数.
     */
    public void setTotalCount(final long totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 根据pageSize与totalCount计算总页数, 默认值为-1.
     */
    public long getTotalPages() {
        if (totalCount < 0) {
            return -1;
        }

        long count = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return (pageNo + 1 <= getTotalPages());
    }

    /**
     * 取得下页的页号, 序号从1开始.
     * 当前页为尾页时仍返回尾页序号.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return pageNo + 1;
        } else {
            return pageNo;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return (pageNo - 1 >= 1);
    }

    /**
     * 取得上页的页号, 序号从1开始.
     * 当前页为首页时返回首页序号.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return pageNo - 1;
        } else {
            return pageNo;
        }
    }
    
    /**syq	2018-11-28 10:46:37	**/
    /**
	 * 自定义分页代码，顶部，需要设置好4个参数：页码，每页大小，总页数，总记录数
	 * @param page	
	 * @return
	 */
	public String showpageTop() {
		int pageNo = this.getPageNo();				//当前第几页
		int pageSize = this.getPageSize();			//每页记录数
		long pageTotalPages = this.getTotalPages();	//总页数
		long pageTotalCount = this.getTotalCount();	//总记录数
		int sdz = 5;			//设定显示分页数字的上限
		int loop_start = 1;		//设定分页数字循环的开始值
		int loop_max = sdz;		//设定分页数字循环的最大值
		String ljf = "\r\n";	//连接换行符号
		
		if(pageNo > pageTotalPages){
			pageNo = 1;
		}
		if(pageTotalPages < sdz){
			loop_max = (int)pageTotalPages;
		}
		if(pageNo > sdz){
			loop_start = pageNo - sdz +1;
			loop_max = pageNo;
		}
		//让分页的当前页居中显示
		if(pageNo < (pageTotalPages - 3) && pageNo > sdz){
			loop_start = pageNo - sdz + 1 + 2;
			loop_max = pageNo + 2;
		}
		
		String ps[] = {"10","25","50","100",String.valueOf(pageTotalCount)};
		
		String a1 = "<div class=\"col-md-5 col-sm-6 col-xs-12\" style=\"float: left;margin-top: 5px;\">"+ ljf;
		String a2 = "显示 <select id=\"showPs\" name=\"showPs\">"+ ljf;
		String a3 = "</select> 条"+ ljf;
		String a4 = "</div>"+ ljf;
		String a_center = "";
		
		for (int i = 0; i < ps.length; i++) {
			String showNum = ps[i];
			if(i == (ps.length-1)){
				showNum = "全部";
			}
			
			a_center += "<option value=\"" + ps[i] + "\"";
			if(ps[i].equals(String.valueOf(pageSize))){
				a_center += " selected=\"selected\"";
			}
			a_center += ">" + showNum + "</option>";
		}
		
		String a = a1 + a2 + a_center + a3 + a4 + ljf;
		String b = "<span class=\"dataTables_paginate paging_simple_numbers\">"+ ljf;
		String c = "<ul class=\"pagination\" style=\"margin:0;\">"+ ljf;
		String y = "</ul>"+ ljf;
		String z = "</span>";
		String li_a = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn(1);\">首页</a></li>"+ ljf;
		String li_b = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+(pageNo-1)+");\">上页</a></li>"+ ljf;
		String li_center = "";
		String li_c = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+(pageNo+1)+");\">下页</a></li>"+ ljf;
		String li_d = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+pageTotalPages+");\">末页</a></li>"+ ljf;
		
		if(pageTotalCount == 0){
			//没有查询到记录，首页，末页，上页，下页，都不可用
			li_a = "<li class=\"paginate_button disabled\"><a>首页</a></li>"+ ljf;
			li_b = "<li class=\"paginate_button disabled\"><a>上页</a></li>"+ ljf;
			li_c = "<li class=\"paginate_button disabled\"><a>下页</a></li>"+ ljf;
			li_d = "<li class=\"paginate_button disabled\"><a>末页</a></li>"+ ljf;
		}else{
			if(pageNo == 1){
				li_a = "<li class=\"paginate_button disabled\"><a>首页</a></li>"+ ljf;
				li_b = "<li class=\"paginate_button disabled\"><a>上页</a></li>"+ ljf;
			}
			if(pageNo == pageTotalPages){
				li_c = "<li class=\"paginate_button disabled\"><a>下页</a></li>"+ ljf;
				li_d = "<li class=\"paginate_button disabled\"><a>末页</a></li>"+ ljf;
			}
		}
		
		for (int i = loop_start; i <= loop_max; i++) {
			String li_center_a = "<li class=\"paginate_button";
			if(pageNo == i ){
				li_center_a += " active\"";	//当前页选中
			}else{
				li_center_a += "\"";
			}
			String li_center_b = "><a href=\"javascript:pageTurn("+i+");\">"+i+"</a></li>";
			li_center = li_center + li_center_a + li_center_b + ljf;
		}
	
		String showpage = a + b + c +li_a + li_b + li_center + li_c + li_d + y + z;
		
		return showpage;
	}
	
	/**
	 * 自定义分页代码，底部，需要设置好3个参数：页码，总页数，总记录数
	 * @param page
	 * @return
	 */
	public String showpageBottom() {
		int pageNo = this.getPageNo();				//当前第几页
		long pageTotalPages = this.getTotalPages();	//总页数
		long pageTotalCount = this.getTotalCount();	//总记录数
		int sdz = 5;			//设定显示分页数字的上限
		int loop_start = 1;		//设定分页数字循环的开始值
		int loop_max = sdz;		//设定分页数字循环的最大值
		String ljf = "\r\n";	//连接换行符号
		
		if(pageNo > pageTotalPages){
			pageNo = 1;
		}
		if(pageTotalPages < sdz){
			loop_max = (int)pageTotalPages;
		}
		if(pageNo > sdz){
			loop_start = pageNo - sdz +1;
			loop_max = pageNo;
		}
		//让分页的当前页居中显示
		if(pageNo < (pageTotalPages - 3) && pageNo > sdz){
			loop_start = pageNo - sdz + 1 + 2;
			loop_max = pageNo + 2;
		}
		
		String a = "<span class=\"dataTables_paginate paging_simple_numbers\" style=\"float: left;\">共 " + pageTotalCount + " 条数据</span>"+ ljf;
		String b = "<span class=\"dataTables_paginate paging_simple_numbers\">"+ ljf;
		String c = "<ul class=\"pagination\" style=\"margin:0;\">"+ ljf;
		String y = "</ul>"+ ljf;
		String z = "</span>";
		String li_a = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn(1);\">首页</a></li>"+ ljf;
		String li_b = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+(pageNo-1)+");\">上页</a></li>"+ ljf;
		String li_center = "";
		String li_c = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+(pageNo+1)+");\">下页</a></li>"+ ljf;
		String li_d = "<li class=\"paginate_button\"><a href=\"javascript:pageTurn("+pageTotalPages+");\">末页</a></li>"+ ljf;
		
		if(pageTotalCount == 0){
			//没有查询到记录，首页，末页，上页，下页，都不可用
			li_a = "<li class=\"paginate_button disabled\"><a>首页</a></li>"+ ljf;
			li_b = "<li class=\"paginate_button disabled\"><a>上页</a></li>"+ ljf;
			li_c = "<li class=\"paginate_button disabled\"><a>下页</a></li>"+ ljf;
			li_d = "<li class=\"paginate_button disabled\"><a>末页</a></li>"+ ljf;
		}else{
			if(pageNo == 1){
				li_a = "<li class=\"paginate_button disabled\"><a>首页</a></li>"+ ljf;
				li_b = "<li class=\"paginate_button disabled\"><a>上页</a></li>"+ ljf;
			}
			if(pageNo == pageTotalPages){
				li_c = "<li class=\"paginate_button disabled\"><a>下页</a></li>"+ ljf;
				li_d = "<li class=\"paginate_button disabled\"><a>末页</a></li>"+ ljf;
			}
		}
		
		for (int i = loop_start; i <= loop_max; i++) {
			String li_center_a = "<li class=\"paginate_button";
			if(pageNo == i ){
				li_center_a += " active\"";	//当前页选中
			}else{
				li_center_a += "\"";
			}
			String li_center_b = "><a href=\"javascript:pageTurn("+i+");\">"+i+"</a></li>";
			li_center = li_center + li_center_a + li_center_b + ljf;
		}
	
		String showpage = a + b + c +li_a + li_b + li_center + li_c + li_d + y + z;
		
		return showpage;
	}
	
}